package com.xinzhi.savingpot.task.service.impl;

import com.xinzhi.savingpot.enums.StatusCode;
import com.xinzhi.savingpot.task.entity.TaskInfo;
import com.xinzhi.savingpot.task.mapper.TaskListMapper;
import com.xinzhi.savingpot.task.service.ITaskListService;
import com.xinzhi.savingpot.task.vo.TaskInfoVo;
import com.xinzhi.savingpot.task.vo.TaskListVo;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.ImageUtil;
import com.xinzhi.savingpot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class TaskListServiceImpl implements ITaskListService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ImageUtil imageUtil;
    @Autowired
    private TaskListMapper taskListMapper;

//    获取默认任务列表
    @Override
    public BaseResponse TaskListPreheat() throws IOException {
//        if (redisUtil.hasKey("任务列表：")){
//            TaskInfo ti = (TaskInfo) redisUtil.get("任务列表：");
//            return new BaseResponse(1, "获取成功", ti);
//        }
        List<TaskInfo> taskList = taskListMapper.quertTaskList1();
        if (taskList.isEmpty()) {
            return new BaseResponse(400, "获取失败");
        }
        Map<String, Object> map = new HashMap();
        TaskListVo tlv;
        List<TaskListVo> listVos = new ArrayList<>();
        for (TaskInfo ti: taskList) {
            if (ti.getEndTime().getTime() < System.currentTimeMillis()){ continue;}
            tlv = new TaskListVo();
            tlv.setUserId(ti.getUserInfo().getUserId());
            tlv.setTaskTitle(ti.getTaskTitle());
            tlv.setTaskId(ti.getTaskId());
            tlv.setUserImg(imageUtil.image2Base64(ti.getUserInfo().getUserHeadPhoto()));
            tlv.setTaskUnitPrice(ti.getTaskUnitPrice());
            tlv.setTaskClassName(ti.getTaskClass().getTaskClassName());
            tlv.setRemainPlaces(ti.getRemainPlaces());
            map.put(ti.getTaskNumber(),tlv);
            listVos.add(tlv);
            redisUtil.hset("任务列表：", ti.getTaskTitle(),tlv,2000);

        }

        return new BaseResponse(200, "获取成功",listVos);
    }


    @Override
    public BaseResponse queryTaskListByClassId(Integer taskClassId) throws IOException {
        if (!redisUtil.hasKey("taskClassList-" + taskClassId)) {
            List<TaskInfo> tis = taskListMapper.quertTaskListByTaskType(taskClassId);
            if (!tis.isEmpty()) {
                TaskListVo tlv;
                List<TaskListVo> tlvs = new ArrayList<>();
                for (TaskInfo ti: tis) {
                    if (ti.getEndTime().getTime() < System.currentTimeMillis()){ continue;}
                    tlv = new TaskListVo();
                    tlv.setTaskId(ti.getTaskId());
                    tlv.setUserId(ti.getUserInfo().getUserId());
                    tlv.setUserImg(imageUtil.image2Base64(ti.getUserInfo().getUserHeadPhoto()));
                    tlv.setRemainPlaces(ti.getRemainPlaces());
                    tlv.setSumPlaces(ti.getSumPlaces());
                    tlv.setTaskTitle(ti.getTaskTitle());
                    tlv.setTaskUnitPrice(ti.getTaskUnitPrice());
                    tlvs.add(tlv);
                }
                redisUtil.set("taskClassList-" + taskClassId , tlvs,600);
                return new BaseResponse(1, "获取成功", tlvs);
            } else {
                return new BaseResponse(1, "获取失败", null);
            }

        }
        List<TaskListVo> tlvs = (List<TaskListVo>) redisUtil.get("taskClassList-" + taskClassId);
        return new BaseResponse(1, "获取成功", tlvs);
    }

    public BaseResponse taskSelectPage() {

        List<TaskInfo> taskinfos = taskListMapper.taskLimitPage(0, 20);
        if(!taskinfos.isEmpty()){
            return new BaseResponse (200,"获取成功",taskinfos);
        }

        return new BaseResponse (StatusCode.Fail);
    }
}
